package com.untrance.shop.core.factory.bean;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.untrance.shop.common.filters.model.CommentFilter;
import com.untrance.shop.common.filters.model.ProductsCatalogFilter;
import com.untrance.shop.core.builder.query.CommentQueryBuilder;
import org.springframework.stereotype.Component;

import com.google.appengine.repackaged.com.google.common.base.Preconditions;
import com.untrance.shop.common.filters.model.Filter;
import com.untrance.shop.core.builder.query.ProductsCatalogQueryBuilder;
import com.untrance.shop.core.builder.query.QueryBuilder;
import com.untrance.shop.core.factory.QueryBuilderFactory;

/**
 * @author eugene zadyra
 */
@Component("queryBuilderFactory")
public class QueryBuilderFactoryBean implements QueryBuilderFactory{
	
	@PersistenceContext
	private EntityManager em;

	public QueryBuilder getQueryBuilder(Filter filter) {
		Preconditions.checkNotNull(filter, "Filter can not be NULL");
		
		if (filter.getClass().getName().equals(ProductsCatalogFilter.class.getName())) {
			return new ProductsCatalogQueryBuilder((ProductsCatalogFilter) filter, em);
		}else if (filter.getClass().getName().equals(CommentFilter.class.getName())) {
			return new CommentQueryBuilder((CommentFilter) filter, em);
		}

		throw new UnsupportedOperationException("Unsupported filter "+filter.getClass().getSimpleName());
	}
}
